package com.itheima.demo6;

import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpUtil;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

public class Demo2 {
    public static void main(String[] args) throws IOException {
        //1.定义变量记录网址
        String familyNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0";
        String boyNameNet = "http://www.haoming8.cn/baobao/10881.html";
        String girlNameNet = "http://www.haoming8.cn/baobao/7641.html";

        //2.爬取数据
        String snn = HttpUtil.get(familyNameNet);
        String bnn =  HttpUtil.get(boyNameNet);
        String gnn =  HttpUtil.get(girlNameNet);

        //3.利用正则表达式获取数据
        List<String> familyTempList = ReUtil.findAll("(.{4})(，|。)", snn, 1);
        List<String> boyTempList = ReUtil.findAll("([\\u4E00-\\u9FA5]{2})(、|。)", bnn, 1);
        List<String> girlTempList = ReUtil.findAll("(.. ){4}..", gnn, 0);

        //处理数据
        ArrayList<String> familyList = new ArrayList<>();
        ArrayList<String> boyList = new ArrayList<>();
        ArrayList<String> girlList = new ArrayList<>();

        //姓的遍历
        for (String s : familyTempList) {
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                familyList.add(c + "");
            }
        }

        //男生名字的遍历: 去重
        for (String s : boyTempList) {
            if (!boyList.contains(s)){
                boyList.add(s);
            }
        }

        //女生的名字的遍历：切割
        for (String s : girlTempList) {
            String[] s1 = s.split(" ");
            for (int i = 0; i < s1.length; i++) {
                girlList.add(s1[i]);
            }
        }


        //生成数据
        //姓名（唯一）-性别-年龄
        ArrayList<String> list = getInfos(familyList,boyList,girlList,7,3);
        Collections.shuffle(list);
//        System.out.println(list);


        //写出数据
        BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
        for (String s : list) {
            bw.write(s);
            bw.newLine();
        }
        bw.close();

    }

    /**
     * 作用：
     *      获取男生和女生的信息：张三-男-23
     * 形参：
     *      1.装着姓氏的集合
     *      2.装置男生名字的集合
     *      3.装着女生名字的集合
     *      4.男生的个数
     *      5.女生的个数
     * @return
     */
    public static ArrayList<String> getInfos(ArrayList<String> familyList,ArrayList<String> boyList,ArrayList<String> girlList,int boyCount,int girlCount){
        //1.生成男生不重复的名字
        HashSet<String> boyhs = hashSet(familyList, boyList, boyCount);
        //2.生成女生不重复的名字
        HashSet<String> girlhs = hashSet(familyList, girlList, girlCount);

        ArrayList<String> list = new ArrayList<>();

        //生成男生的信息并添加到集合中
        Random r = new Random();
        for (String boyName : boyhs) {
            int age = r.nextInt(10) + 18;
            list.add(boyName + "-男-" + age);
        }

        //生成男生的信息并添加到集合中
        for (String girlName : girlhs) {
            int age = r.nextInt(8) + 18;
            list.add(girlName + "-女-" + age);
        }


        return list;

    }

    //生成名字的方法
    private static HashSet<String> hashSet(ArrayList<String> familyList, ArrayList<String> List, int count) {

        HashSet<String> hs = new HashSet<>();
        while (true){
            //判断集合长度是否足够
            if (hs.size() == count){
                break;
            }
            //随机生成名字并添加
            Collections.shuffle(familyList);
            Collections.shuffle(List);
            hs.add(familyList.get(0)+List.get(0));
        }

        return hs;
    }

}
